{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "06b098ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import itertools\n",
    "import joblib\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. Generate Mechanism Database"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4104d58f",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Number of Reactions</th>\n",
       "      <th>Step</th>\n",
       "      <th>SM_left</th>\n",
       "      <th>C_left</th>\n",
       "      <th>B_left</th>\n",
       "      <th>P_right</th>\n",
       "      <th>IMP1_right</th>\n",
       "      <th>INT1_right</th>\n",
       "      <th>C_right</th>\n",
       "      <th>B_right</th>\n",
       "      <th>R_left</th>\n",
       "      <th>P_left</th>\n",
       "      <th>INT1_left</th>\n",
       "      <th>IMP2_right</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1531</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1532</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1533</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1534</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1535</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1536 rows × 14 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      Number of Reactions  Step  SM_left  C_left  B_left  P_right  IMP1_right  \\\n",
       "0                       2     1        0       0       0        0           0   \n",
       "1                       2     1        0       0       0        0           0   \n",
       "2                       2     1        0       0       0        0           0   \n",
       "3                       2     1        0       0       0        0           0   \n",
       "4                       2     1        0       0       0        0           0   \n",
       "...                   ...   ...      ...     ...     ...      ...         ...   \n",
       "1531                    2     2        1       1       1        1           0   \n",
       "1532                    2     2        1       1       1        1           0   \n",
       "1533                    2     2        1       1       1        1           0   \n",
       "1534                    2     2        1       1       1        1           0   \n",
       "1535                    2     2        1       1       1        1           0   \n",
       "\n",
       "      INT1_right  C_right  B_right  R_left  P_left  INT1_left  IMP2_right  \n",
       "0              0        0        0       0       0          0           0  \n",
       "1              0        0        0       1       0          0           0  \n",
       "2              0        0        1       0       0          0           0  \n",
       "3              0        0        1       1       0          0           0  \n",
       "4              0        1        0       0       0          0           0  \n",
       "...          ...      ...      ...     ...     ...        ...         ...  \n",
       "1531           0        0        1       1       1          1           1  \n",
       "1532           0        1        0       0       1          1           1  \n",
       "1533           0        1        0       1       1          1           1  \n",
       "1534           0        1        1       0       1          1           1  \n",
       "1535           0        1        1       1       1          1           1  \n",
       "\n",
       "[1536 rows x 14 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "binary_values = [0, 1]\n",
    "\n",
    "values_for_R_left = [0, 1]\n",
    "\n",
    "# Two reactions\n",
    "two_rxn_1 = ['SM_left', 'C_left', 'B_left', 'P_right', 'IMP1_right', 'INT1_right', 'C_right', 'B_right', 'R_left']\n",
    "two_rxn_2 = ['SM_left', 'C_left', 'B_left', 'P_left', 'INT1_left', 'P_right', 'IMP2_right', 'C_right', 'B_right', 'R_left']\n",
    "\n",
    "\n",
    "# Create all combinations of 0 or 1 for the columns except 'R_left' and create all combinations of 0, 1, or 2 for 'R_left'\n",
    "combinations_R_left = list(itertools.product(values_for_R_left, repeat=1))\n",
    "\n",
    "\n",
    "combinations2_1 = list(itertools.product(binary_values, repeat=len(two_rxn_1) - 1))\n",
    "combinations2_1 = [comb + comb_R_left for comb in combinations2_1 for comb_R_left in combinations_R_left]\n",
    "combinations2_2 = list(itertools.product(binary_values, repeat=len(two_rxn_2) - 1))\n",
    "combinations2_2 = [comb + comb_R_left for comb in combinations2_2 for comb_R_left in combinations_R_left]\n",
    "\n",
    "columns=two_rxn_1[:-1]  + ['R_left']\n",
    "df2_1 = pd.DataFrame(combinations2_1, columns=columns)\n",
    "columns=two_rxn_2[:-1]  + ['R_left']\n",
    "df2_2 = pd.DataFrame(combinations2_2, columns=columns)\n",
    "\n",
    "df2_1['Number of Reactions'] = 2\n",
    "df2_1['Step'] = 1\n",
    "df2_2['Number of Reactions'] = 2\n",
    "df2_2['Step'] = 2\n",
    "\n",
    "# Combine all DataFrames\n",
    "df_combined = pd.concat([df2_1, df2_2], ignore_index=True)\n",
    "\n",
    "# Fill NaN (missing values) with 0\n",
    "df_combined = df_combined.fillna(0).astype(int)\n",
    "\n",
    "columns_to_move = [\"Number of Reactions\", \"Step\"]\n",
    "\n",
    "# Create a list of columns in the original order (excluding the ones to move)\n",
    "columns_in_original_order = [col for col in df_combined.columns if col not in columns_to_move]\n",
    "\n",
    "# Reorder the columns in the desired order\n",
    "new_order = columns_to_move + columns_in_original_order\n",
    "\n",
    "df_combined = df_combined[new_order]\n",
    "df_combined\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e464a8c3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Number of Reactions</th>\n",
       "      <th>Step</th>\n",
       "      <th>SM_left</th>\n",
       "      <th>C_left</th>\n",
       "      <th>B_left</th>\n",
       "      <th>P_right</th>\n",
       "      <th>IMP1_right</th>\n",
       "      <th>INT1_right</th>\n",
       "      <th>C_right</th>\n",
       "      <th>B_right</th>\n",
       "      <th>R_left</th>\n",
       "      <th>P_left</th>\n",
       "      <th>INT1_left</th>\n",
       "      <th>IMP2_right</th>\n",
       "      <th>Reaction Format</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>-&gt;</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>R -&gt;</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>-&gt; B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>R -&gt; B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>-&gt; C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1531</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + R + INT1 -&gt; P + IMP2 + B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1532</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + INT1 -&gt; P + IMP2 + C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1533</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + R + INT1 -&gt; P + IMP2 + C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1534</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + INT1 -&gt; P + IMP2 + C + B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1535</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + R + INT1 -&gt; P + IMP2 + C + B</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1536 rows × 15 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      Number of Reactions  Step  SM_left  C_left  B_left  P_right  IMP1_right  \\\n",
       "0                       2     1        0       0       0        0           0   \n",
       "1                       2     1        0       0       0        0           0   \n",
       "2                       2     1        0       0       0        0           0   \n",
       "3                       2     1        0       0       0        0           0   \n",
       "4                       2     1        0       0       0        0           0   \n",
       "...                   ...   ...      ...     ...     ...      ...         ...   \n",
       "1531                    2     2        1       1       1        1           0   \n",
       "1532                    2     2        1       1       1        1           0   \n",
       "1533                    2     2        1       1       1        1           0   \n",
       "1534                    2     2        1       1       1        1           0   \n",
       "1535                    2     2        1       1       1        1           0   \n",
       "\n",
       "      INT1_right  C_right  B_right  R_left  P_left  INT1_left  IMP2_right  \\\n",
       "0              0        0        0       0       0          0           0   \n",
       "1              0        0        0       1       0          0           0   \n",
       "2              0        0        1       0       0          0           0   \n",
       "3              0        0        1       1       0          0           0   \n",
       "4              0        1        0       0       0          0           0   \n",
       "...          ...      ...      ...     ...     ...        ...         ...   \n",
       "1531           0        0        1       1       1          1           1   \n",
       "1532           0        1        0       0       1          1           1   \n",
       "1533           0        1        0       1       1          1           1   \n",
       "1534           0        1        1       0       1          1           1   \n",
       "1535           0        1        1       1       1          1           1   \n",
       "\n",
       "                                    Reaction Format  \n",
       "0                                               ->   \n",
       "1                                             R ->   \n",
       "2                                              -> B  \n",
       "3                                            R -> B  \n",
       "4                                              -> C  \n",
       "...                                             ...  \n",
       "1531      SM + P + C + B + R + INT1 -> P + IMP2 + B  \n",
       "1532          SM + P + C + B + INT1 -> P + IMP2 + C  \n",
       "1533      SM + P + C + B + R + INT1 -> P + IMP2 + C  \n",
       "1534      SM + P + C + B + INT1 -> P + IMP2 + C + B  \n",
       "1535  SM + P + C + B + R + INT1 -> P + IMP2 + C + B  \n",
       "\n",
       "[1536 rows x 15 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Function to convert a row to reaction format\n",
    "def row_to_reaction(row):\n",
    "    reaction_parts_left = []\n",
    "    reaction_parts_right = []\n",
    "\n",
    "    if row['SM_left'] > 0:\n",
    "        reaction_parts_left.append('SM')\n",
    "    if row['P_left'] > 0:\n",
    "        reaction_parts_left.append('P')\n",
    "    if row['C_left'] > 0:\n",
    "        reaction_parts_left.append('C')\n",
    "    if row['B_left'] > 0:\n",
    "        reaction_parts_left.append('B')\n",
    "    if row['P_right'] > 0:\n",
    "        reaction_parts_right.append('P')\n",
    "    if row['IMP1_right'] > 0:\n",
    "        reaction_parts_right.append('IMP1')\n",
    "    if row['IMP2_right'] > 0:\n",
    "        reaction_parts_right.append('IMP2')\n",
    "    if row['C_right'] > 0:\n",
    "        reaction_parts_right.append('C')\n",
    "    if row['B_right'] > 0:\n",
    "        reaction_parts_right.append('B')\n",
    "    if row['R_left'] == 1:\n",
    "        reaction_parts_left.append('R')\n",
    "    if row['INT1_left'] > 0:\n",
    "        reaction_parts_left.append('INT1')\n",
    "    if row['INT1_right'] > 0:\n",
    "        reaction_parts_right.append('INT1')\n",
    "\n",
    "    reaction_str_left = ' + '.join(reaction_parts_left)\n",
    "    reaction_str_right = ' + '.join(reaction_parts_right)\n",
    "\n",
    "    reaction_str = reaction_str_left + ' -> ' + reaction_str_right\n",
    "    return reaction_str\n",
    "\n",
    "# Apply the function to each row and create the 'Reaction Format' column\n",
    "df_combined['Reaction Format'] = df_combined.apply(row_to_reaction, axis=1)\n",
    "\n",
    "df_combined"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of rows where Step is equal to '1': 512\n",
      "Number of rows where Step is equal to '2': 1024\n"
     ]
    }
   ],
   "source": [
    "count_rows = len(df_combined[df_combined['Step'] == 1])\n",
    "print(f'Number of rows where Step is equal to \\'1\\': {count_rows}')\n",
    "\n",
    "count_rows2 = len(df_combined[df_combined['Step'] == 2])\n",
    "print(f'Number of rows where Step is equal to \\'2\\': {count_rows2}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9ca2ae60",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\I0512620\\AppData\\Local\\Temp\\ipykernel_30000\\1947159669.py:13: UserWarning: Boolean Series key will be reindexed to match DataFrame index.\n",
      "  df_combined = df_combined[row_sums_right > 0]\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Number of Reactions</th>\n",
       "      <th>Step</th>\n",
       "      <th>SM_left</th>\n",
       "      <th>C_left</th>\n",
       "      <th>B_left</th>\n",
       "      <th>P_right</th>\n",
       "      <th>IMP1_right</th>\n",
       "      <th>INT1_right</th>\n",
       "      <th>C_right</th>\n",
       "      <th>B_right</th>\n",
       "      <th>R_left</th>\n",
       "      <th>P_left</th>\n",
       "      <th>INT1_left</th>\n",
       "      <th>IMP2_right</th>\n",
       "      <th>Reaction Format</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>R -&gt; B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>R -&gt; C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>R -&gt; C + B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>R -&gt; INT1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>R -&gt; B + INT1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1531</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + R + INT1 -&gt; P + IMP2 + B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1532</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + INT1 -&gt; P + IMP2 + C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1533</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + R + INT1 -&gt; P + IMP2 + C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1534</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + INT1 -&gt; P + IMP2 + C + B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1535</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + R + INT1 -&gt; P + IMP2 + C + B</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1410 rows × 15 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      Number of Reactions  Step  SM_left  C_left  B_left  P_right  IMP1_right  \\\n",
       "3                       2     1        0       0       0        0           0   \n",
       "5                       2     1        0       0       0        0           0   \n",
       "7                       2     1        0       0       0        0           0   \n",
       "9                       2     1        0       0       0        0           0   \n",
       "11                      2     1        0       0       0        0           0   \n",
       "...                   ...   ...      ...     ...     ...      ...         ...   \n",
       "1531                    2     2        1       1       1        1           0   \n",
       "1532                    2     2        1       1       1        1           0   \n",
       "1533                    2     2        1       1       1        1           0   \n",
       "1534                    2     2        1       1       1        1           0   \n",
       "1535                    2     2        1       1       1        1           0   \n",
       "\n",
       "      INT1_right  C_right  B_right  R_left  P_left  INT1_left  IMP2_right  \\\n",
       "3              0        0        1       1       0          0           0   \n",
       "5              0        1        0       1       0          0           0   \n",
       "7              0        1        1       1       0          0           0   \n",
       "9              1        0        0       1       0          0           0   \n",
       "11             1        0        1       1       0          0           0   \n",
       "...          ...      ...      ...     ...     ...        ...         ...   \n",
       "1531           0        0        1       1       1          1           1   \n",
       "1532           0        1        0       0       1          1           1   \n",
       "1533           0        1        0       1       1          1           1   \n",
       "1534           0        1        1       0       1          1           1   \n",
       "1535           0        1        1       1       1          1           1   \n",
       "\n",
       "                                    Reaction Format  \n",
       "3                                            R -> B  \n",
       "5                                            R -> C  \n",
       "7                                        R -> C + B  \n",
       "9                                         R -> INT1  \n",
       "11                                    R -> B + INT1  \n",
       "...                                             ...  \n",
       "1531      SM + P + C + B + R + INT1 -> P + IMP2 + B  \n",
       "1532          SM + P + C + B + INT1 -> P + IMP2 + C  \n",
       "1533      SM + P + C + B + R + INT1 -> P + IMP2 + C  \n",
       "1534      SM + P + C + B + INT1 -> P + IMP2 + C + B  \n",
       "1535  SM + P + C + B + R + INT1 -> P + IMP2 + C + B  \n",
       "\n",
       "[1410 rows x 15 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Criteria 1: Each step must have at least one species on the left or right side of the reaction.\n",
    "\n",
    "# Select columns that end with \"_left\" and \"_right\"\n",
    "left_columns = df_combined.filter(like=\"_left\")\n",
    "right_columns = df_combined.filter(like=\"_right\")\n",
    "\n",
    "# Calculate the sum of each row for the selected columns\n",
    "row_sums_left = left_columns.sum(axis=1)\n",
    "row_sums_right = right_columns.sum(axis=1)\n",
    "\n",
    "# Filter rows where the sum of \"_left\" and \"_right\" columns are not zero\n",
    "df_combined = df_combined[row_sums_left > 0]\n",
    "df_combined = df_combined[row_sums_right > 0]\n",
    "\n",
    "df_combined"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "343cb579",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Number of Reactions</th>\n",
       "      <th>Step</th>\n",
       "      <th>SM_left</th>\n",
       "      <th>C_left</th>\n",
       "      <th>B_left</th>\n",
       "      <th>P_right</th>\n",
       "      <th>IMP1_right</th>\n",
       "      <th>INT1_right</th>\n",
       "      <th>C_right</th>\n",
       "      <th>B_right</th>\n",
       "      <th>R_left</th>\n",
       "      <th>P_left</th>\n",
       "      <th>INT1_left</th>\n",
       "      <th>IMP2_right</th>\n",
       "      <th>Reaction Format</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>R -&gt; INT1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>R -&gt; B + INT1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>R -&gt; C + INT1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>R -&gt; C + B + INT1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>R -&gt; IMP1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1531</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + R + INT1 -&gt; P + IMP2 + B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1532</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + INT1 -&gt; P + IMP2 + C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1533</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + R + INT1 -&gt; P + IMP2 + C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1534</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + INT1 -&gt; P + IMP2 + C + B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1535</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + R + INT1 -&gt; P + IMP2 + C + B</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1056 rows × 15 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      Number of Reactions  Step  SM_left  C_left  B_left  P_right  IMP1_right  \\\n",
       "9                       2     1        0       0       0        0           0   \n",
       "11                      2     1        0       0       0        0           0   \n",
       "13                      2     1        0       0       0        0           0   \n",
       "15                      2     1        0       0       0        0           0   \n",
       "17                      2     1        0       0       0        0           1   \n",
       "...                   ...   ...      ...     ...     ...      ...         ...   \n",
       "1531                    2     2        1       1       1        1           0   \n",
       "1532                    2     2        1       1       1        1           0   \n",
       "1533                    2     2        1       1       1        1           0   \n",
       "1534                    2     2        1       1       1        1           0   \n",
       "1535                    2     2        1       1       1        1           0   \n",
       "\n",
       "      INT1_right  C_right  B_right  R_left  P_left  INT1_left  IMP2_right  \\\n",
       "9              1        0        0       1       0          0           0   \n",
       "11             1        0        1       1       0          0           0   \n",
       "13             1        1        0       1       0          0           0   \n",
       "15             1        1        1       1       0          0           0   \n",
       "17             0        0        0       1       0          0           0   \n",
       "...          ...      ...      ...     ...     ...        ...         ...   \n",
       "1531           0        0        1       1       1          1           1   \n",
       "1532           0        1        0       0       1          1           1   \n",
       "1533           0        1        0       1       1          1           1   \n",
       "1534           0        1        1       0       1          1           1   \n",
       "1535           0        1        1       1       1          1           1   \n",
       "\n",
       "                                    Reaction Format  \n",
       "9                                         R -> INT1  \n",
       "11                                    R -> B + INT1  \n",
       "13                                    R -> C + INT1  \n",
       "15                                R -> C + B + INT1  \n",
       "17                                        R -> IMP1  \n",
       "...                                             ...  \n",
       "1531      SM + P + C + B + R + INT1 -> P + IMP2 + B  \n",
       "1532          SM + P + C + B + INT1 -> P + IMP2 + C  \n",
       "1533      SM + P + C + B + R + INT1 -> P + IMP2 + C  \n",
       "1534      SM + P + C + B + INT1 -> P + IMP2 + C + B  \n",
       "1535  SM + P + C + B + R + INT1 -> P + IMP2 + C + B  \n",
       "\n",
       "[1056 rows x 15 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Criteria 2: Each step should have at least one species other than the base and catalyst.\n",
    "\n",
    "left_columns = df_combined.filter(like=\"_left\")\n",
    "row_sums_left = left_columns.sum(axis=1)\n",
    "\n",
    "right_columns = df_combined.filter(like=\"_right\")\n",
    "row_sums_right = right_columns.sum(axis=1)\n",
    "\n",
    "# Convert specific columns to int if needed\n",
    "df_combined['B_left'] = df_combined['B_left'].astype(int)\n",
    "df_combined['C_left'] = df_combined['C_left'].astype(int)\n",
    "df_combined['B_right'] = df_combined['B_right'].astype(int)\n",
    "df_combined['C_right'] = df_combined['C_right'].astype(int)\n",
    "\n",
    "# Create conditions\n",
    "condition1 = (df_combined['B_left'] == 1) & (row_sums_left == 1)\n",
    "condition2 = (df_combined['B_right'] == 1) & (row_sums_right == 1)\n",
    "condition3 = (df_combined['C_left'] == 1) & (row_sums_left == 1)\n",
    "condition4 = (df_combined['C_right'] == 1) & (row_sums_right == 1)\n",
    "condition5 = (df_combined['B_left'] == 1) & (df_combined['C_left'] == 1) & (row_sums_left == 2)\n",
    "condition6 = (df_combined['B_right'] == 1) & (df_combined['C_right'] == 1) & (row_sums_right == 2)\n",
    "\n",
    "# Combine conditions with logical OR\n",
    "final_condition = np.logical_or.reduce([condition1, condition2, condition3, condition4, condition5, condition6])\n",
    "\n",
    "# Apply the final condition to filter the DataFrame\n",
    "df_combined = df_combined[~final_condition]\n",
    "df_combined"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8f3b5c62",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Number of Reactions</th>\n",
       "      <th>Step</th>\n",
       "      <th>SM_left</th>\n",
       "      <th>C_left</th>\n",
       "      <th>B_left</th>\n",
       "      <th>P_right</th>\n",
       "      <th>IMP1_right</th>\n",
       "      <th>INT1_right</th>\n",
       "      <th>C_right</th>\n",
       "      <th>B_right</th>\n",
       "      <th>R_left</th>\n",
       "      <th>P_left</th>\n",
       "      <th>INT1_left</th>\n",
       "      <th>IMP2_right</th>\n",
       "      <th>Reaction Format</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>264</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>SM -&gt; INT1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>265</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>SM + R -&gt; INT1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>272</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>SM -&gt; IMP1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>273</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>SM + R -&gt; IMP1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>280</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>SM -&gt; IMP1 + INT1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1515</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + R + INT1 -&gt; IMP2 + B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1516</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + INT1 -&gt; IMP2 + C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1517</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + R + INT1 -&gt; IMP2 + C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1518</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + INT1 -&gt; IMP2 + C + B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1519</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + R + INT1 -&gt; IMP2 + C + B</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>590 rows × 15 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      Number of Reactions  Step  SM_left  C_left  B_left  P_right  IMP1_right  \\\n",
       "264                     2     1        1       0       0        0           0   \n",
       "265                     2     1        1       0       0        0           0   \n",
       "272                     2     1        1       0       0        0           1   \n",
       "273                     2     1        1       0       0        0           1   \n",
       "280                     2     1        1       0       0        0           1   \n",
       "...                   ...   ...      ...     ...     ...      ...         ...   \n",
       "1515                    2     2        1       1       1        0           0   \n",
       "1516                    2     2        1       1       1        0           0   \n",
       "1517                    2     2        1       1       1        0           0   \n",
       "1518                    2     2        1       1       1        0           0   \n",
       "1519                    2     2        1       1       1        0           0   \n",
       "\n",
       "      INT1_right  C_right  B_right  R_left  P_left  INT1_left  IMP2_right  \\\n",
       "264            1        0        0       0       0          0           0   \n",
       "265            1        0        0       1       0          0           0   \n",
       "272            0        0        0       0       0          0           0   \n",
       "273            0        0        0       1       0          0           0   \n",
       "280            1        0        0       0       0          0           0   \n",
       "...          ...      ...      ...     ...     ...        ...         ...   \n",
       "1515           0        0        1       1       1          1           1   \n",
       "1516           0        1        0       0       1          1           1   \n",
       "1517           0        1        0       1       1          1           1   \n",
       "1518           0        1        1       0       1          1           1   \n",
       "1519           0        1        1       1       1          1           1   \n",
       "\n",
       "                                Reaction Format  \n",
       "264                                  SM -> INT1  \n",
       "265                              SM + R -> INT1  \n",
       "272                                  SM -> IMP1  \n",
       "273                              SM + R -> IMP1  \n",
       "280                           SM -> IMP1 + INT1  \n",
       "...                                         ...  \n",
       "1515      SM + P + C + B + R + INT1 -> IMP2 + B  \n",
       "1516          SM + P + C + B + INT1 -> IMP2 + C  \n",
       "1517      SM + P + C + B + R + INT1 -> IMP2 + C  \n",
       "1518      SM + P + C + B + INT1 -> IMP2 + C + B  \n",
       "1519  SM + P + C + B + R + INT1 -> IMP2 + C + B  \n",
       "\n",
       "[590 rows x 15 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def violates(row):\n",
    "    # Criteria 3: The product should not function as both a reactant and product simultaneously in each step.\n",
    "    if ((row['P_left'] == 1 and row['P_right'] == 1) or\n",
    "        # Criteria 4: In the first step of the reaction, the starting material must be present on the left side of the reaction.\n",
    "        (row['Step'] == 1 and row['SM_left'] == 0) or\n",
    "        # Criteria 5: In the first step of the reaction, if there is a base or catalyst on the right side of the reaction, they must also be on the left side of the reaction simultaneously.\n",
    "        (row['Step'] == 1 and row['B_left'] == 0 and row['B_right'] == 1) or \n",
    "        (row['Step'] == 1 and row['C_left'] == 0 and row['C_right'] == 1)\n",
    "       ):\n",
    "        return True\n",
    "    return False\n",
    "\n",
    "# Filter rows that violate the new rule\n",
    "df_combined = df_combined[~df_combined.apply(violates, axis=1)]\n",
    "\n",
    "df_combined"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "87baa6e0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Number of Reactions</th>\n",
       "      <th>Step</th>\n",
       "      <th>SM_left</th>\n",
       "      <th>C_left</th>\n",
       "      <th>B_left</th>\n",
       "      <th>P_right</th>\n",
       "      <th>IMP1_right</th>\n",
       "      <th>INT1_right</th>\n",
       "      <th>C_right</th>\n",
       "      <th>B_right</th>\n",
       "      <th>R_left</th>\n",
       "      <th>P_left</th>\n",
       "      <th>INT1_left</th>\n",
       "      <th>IMP2_right</th>\n",
       "      <th>Reaction Format</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>SM -&gt; INT1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>SM + R -&gt; INT1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>SM -&gt; IMP1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>SM + R -&gt; IMP1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>SM -&gt; IMP1 + INT1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>585</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + R + INT1 -&gt; IMP2 + B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>586</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + INT1 -&gt; IMP2 + C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>587</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + R + INT1 -&gt; IMP2 + C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>588</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + INT1 -&gt; IMP2 + C + B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>589</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + R + INT1 -&gt; IMP2 + C + B</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>590 rows × 15 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Number of Reactions  Step  SM_left  C_left  B_left  P_right  IMP1_right  \\\n",
       "0                      2     1        1       0       0        0           0   \n",
       "1                      2     1        1       0       0        0           0   \n",
       "2                      2     1        1       0       0        0           1   \n",
       "3                      2     1        1       0       0        0           1   \n",
       "4                      2     1        1       0       0        0           1   \n",
       "..                   ...   ...      ...     ...     ...      ...         ...   \n",
       "585                    2     2        1       1       1        0           0   \n",
       "586                    2     2        1       1       1        0           0   \n",
       "587                    2     2        1       1       1        0           0   \n",
       "588                    2     2        1       1       1        0           0   \n",
       "589                    2     2        1       1       1        0           0   \n",
       "\n",
       "     INT1_right  C_right  B_right  R_left  P_left  INT1_left  IMP2_right  \\\n",
       "0             1        0        0       0       0          0           0   \n",
       "1             1        0        0       1       0          0           0   \n",
       "2             0        0        0       0       0          0           0   \n",
       "3             0        0        0       1       0          0           0   \n",
       "4             1        0        0       0       0          0           0   \n",
       "..          ...      ...      ...     ...     ...        ...         ...   \n",
       "585           0        0        1       1       1          1           1   \n",
       "586           0        1        0       0       1          1           1   \n",
       "587           0        1        0       1       1          1           1   \n",
       "588           0        1        1       0       1          1           1   \n",
       "589           0        1        1       1       1          1           1   \n",
       "\n",
       "                               Reaction Format  \n",
       "0                                   SM -> INT1  \n",
       "1                               SM + R -> INT1  \n",
       "2                                   SM -> IMP1  \n",
       "3                               SM + R -> IMP1  \n",
       "4                            SM -> IMP1 + INT1  \n",
       "..                                         ...  \n",
       "585      SM + P + C + B + R + INT1 -> IMP2 + B  \n",
       "586          SM + P + C + B + INT1 -> IMP2 + C  \n",
       "587      SM + P + C + B + R + INT1 -> IMP2 + C  \n",
       "588      SM + P + C + B + INT1 -> IMP2 + C + B  \n",
       "589  SM + P + C + B + R + INT1 -> IMP2 + C + B  \n",
       "\n",
       "[590 rows x 15 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check duplication (not necessary)\n",
    "\n",
    "# Get the subset of columns from the third column to the last column\n",
    "columns_to_check = df_combined.columns[2:]\n",
    "\n",
    "# Check for duplicates based on the specified subset of columns\n",
    "duplicate_mask = df_combined.duplicated(subset=columns_to_check, keep='last')\n",
    "\n",
    "# Keep only the non-duplicate rows (keeping the last occurrence)\n",
    "df_unique = df_combined[~duplicate_mask]\n",
    "\n",
    "# Reset the index without adding the previous index as a new column\n",
    "df_combined.reset_index(drop=True, inplace=True)\n",
    "df_combined\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Add initial species in the dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df_combined.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Number of Reactions</th>\n",
       "      <th>Step</th>\n",
       "      <th>SM_left</th>\n",
       "      <th>C_left</th>\n",
       "      <th>B_left</th>\n",
       "      <th>P_right</th>\n",
       "      <th>IMP1_right</th>\n",
       "      <th>INT1_right</th>\n",
       "      <th>C_right</th>\n",
       "      <th>B_right</th>\n",
       "      <th>R_left</th>\n",
       "      <th>P_left</th>\n",
       "      <th>INT1_left</th>\n",
       "      <th>IMP2_right</th>\n",
       "      <th>Reaction Format</th>\n",
       "      <th>Initial Compounds</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>SM -&gt; INT1</td>\n",
       "      <td>SM</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>SM + R -&gt; INT1</td>\n",
       "      <td>SM,R</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>SM -&gt; IMP1</td>\n",
       "      <td>SM,IMP1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>SM + R -&gt; IMP1</td>\n",
       "      <td>SM,R,IMP1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>SM -&gt; IMP1 + INT1</td>\n",
       "      <td>SM,IMP1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>585</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + R + INT1 -&gt; IMP2 + B</td>\n",
       "      <td>SM,B,C,R</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>586</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + INT1 -&gt; IMP2 + C</td>\n",
       "      <td>SM,B,C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>587</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + R + INT1 -&gt; IMP2 + C</td>\n",
       "      <td>SM,B,C,R</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>588</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + INT1 -&gt; IMP2 + C + B</td>\n",
       "      <td>SM,B,C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>589</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + R + INT1 -&gt; IMP2 + C + B</td>\n",
       "      <td>SM,B,C,R</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>590 rows × 16 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Number of Reactions  Step  SM_left  C_left  B_left  P_right  IMP1_right  \\\n",
       "0                      2     1        1       0       0        0           0   \n",
       "1                      2     1        1       0       0        0           0   \n",
       "2                      2     1        1       0       0        0           1   \n",
       "3                      2     1        1       0       0        0           1   \n",
       "4                      2     1        1       0       0        0           1   \n",
       "..                   ...   ...      ...     ...     ...      ...         ...   \n",
       "585                    2     2        1       1       1        0           0   \n",
       "586                    2     2        1       1       1        0           0   \n",
       "587                    2     2        1       1       1        0           0   \n",
       "588                    2     2        1       1       1        0           0   \n",
       "589                    2     2        1       1       1        0           0   \n",
       "\n",
       "     INT1_right  C_right  B_right  R_left  P_left  INT1_left  IMP2_right  \\\n",
       "0             1        0        0       0       0          0           0   \n",
       "1             1        0        0       1       0          0           0   \n",
       "2             0        0        0       0       0          0           0   \n",
       "3             0        0        0       1       0          0           0   \n",
       "4             1        0        0       0       0          0           0   \n",
       "..          ...      ...      ...     ...     ...        ...         ...   \n",
       "585           0        0        1       1       1          1           1   \n",
       "586           0        1        0       0       1          1           1   \n",
       "587           0        1        0       1       1          1           1   \n",
       "588           0        1        1       0       1          1           1   \n",
       "589           0        1        1       1       1          1           1   \n",
       "\n",
       "                               Reaction Format Initial Compounds  \n",
       "0                                   SM -> INT1                SM  \n",
       "1                               SM + R -> INT1              SM,R  \n",
       "2                                   SM -> IMP1           SM,IMP1  \n",
       "3                               SM + R -> IMP1         SM,R,IMP1  \n",
       "4                            SM -> IMP1 + INT1           SM,IMP1  \n",
       "..                                         ...               ...  \n",
       "585      SM + P + C + B + R + INT1 -> IMP2 + B          SM,B,C,R  \n",
       "586          SM + P + C + B + INT1 -> IMP2 + C            SM,B,C  \n",
       "587      SM + P + C + B + R + INT1 -> IMP2 + C          SM,B,C,R  \n",
       "588      SM + P + C + B + INT1 -> IMP2 + C + B            SM,B,C  \n",
       "589  SM + P + C + B + R + INT1 -> IMP2 + C + B          SM,B,C,R  \n",
       "\n",
       "[590 rows x 16 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# List of columns to check\n",
    "columns_to_check = ['SM_left', 'B_left', 'C_left', 'R_left', 'IMP1_right']\n",
    "\n",
    "# Create a new column to store the short names\n",
    "df['Initial Compounds'] = ''\n",
    "\n",
    "# Iterate through the specified columns\n",
    "for column_name in columns_to_check:\n",
    "    short_name = column_name.split('_')[0]  # Extract short name from column name\n",
    "    mask = df[column_name] > 0  # Create a mask for values greater than 0\n",
    "    df.loc[mask, 'Initial Compounds'] += short_name + ','  # Append short name to the new column\n",
    "\n",
    "# Remove trailing comma from the new column values\n",
    "df['Initial Compounds'] = df['Initial Compounds'].str.rstrip(',')\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['database_two_reactions.joblib']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Save the DataFrame\n",
    "df.to_excel('database_two_reactions.xlsx', index=True)\n",
    "joblib.dump(df, \"database_two_reactions.joblib\", compress=9)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83cb378c",
   "metadata": {},
   "source": [
    "### 2. Generate All Reasonable Combinations for Two Reactions Mechanism"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Number of Reactions</th>\n",
       "      <th>Step</th>\n",
       "      <th>SM_left</th>\n",
       "      <th>C_left</th>\n",
       "      <th>B_left</th>\n",
       "      <th>P_right</th>\n",
       "      <th>IMP1_right</th>\n",
       "      <th>INT1_right</th>\n",
       "      <th>C_right</th>\n",
       "      <th>B_right</th>\n",
       "      <th>R_left</th>\n",
       "      <th>P_left</th>\n",
       "      <th>INT1_left</th>\n",
       "      <th>IMP2_right</th>\n",
       "      <th>Reaction Format</th>\n",
       "      <th>Initial Compounds</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>SM -&gt; INT1</td>\n",
       "      <td>SM</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>SM + R -&gt; INT1</td>\n",
       "      <td>SM,R</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>SM -&gt; IMP1</td>\n",
       "      <td>SM,IMP1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>SM + R -&gt; IMP1</td>\n",
       "      <td>SM,R,IMP1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>SM -&gt; IMP1 + INT1</td>\n",
       "      <td>SM,IMP1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>585</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + R + INT1 -&gt; IMP2 + B</td>\n",
       "      <td>SM,B,C,R</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>586</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + INT1 -&gt; IMP2 + C</td>\n",
       "      <td>SM,B,C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>587</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + R + INT1 -&gt; IMP2 + C</td>\n",
       "      <td>SM,B,C,R</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>588</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + INT1 -&gt; IMP2 + C + B</td>\n",
       "      <td>SM,B,C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>589</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>SM + P + C + B + R + INT1 -&gt; IMP2 + C + B</td>\n",
       "      <td>SM,B,C,R</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>590 rows × 16 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Number of Reactions  Step  SM_left  C_left  B_left  P_right  IMP1_right  \\\n",
       "0                      2     1        1       0       0        0           0   \n",
       "1                      2     1        1       0       0        0           0   \n",
       "2                      2     1        1       0       0        0           1   \n",
       "3                      2     1        1       0       0        0           1   \n",
       "4                      2     1        1       0       0        0           1   \n",
       "..                   ...   ...      ...     ...     ...      ...         ...   \n",
       "585                    2     2        1       1       1        0           0   \n",
       "586                    2     2        1       1       1        0           0   \n",
       "587                    2     2        1       1       1        0           0   \n",
       "588                    2     2        1       1       1        0           0   \n",
       "589                    2     2        1       1       1        0           0   \n",
       "\n",
       "     INT1_right  C_right  B_right  R_left  P_left  INT1_left  IMP2_right  \\\n",
       "0             1        0        0       0       0          0           0   \n",
       "1             1        0        0       1       0          0           0   \n",
       "2             0        0        0       0       0          0           0   \n",
       "3             0        0        0       1       0          0           0   \n",
       "4             1        0        0       0       0          0           0   \n",
       "..          ...      ...      ...     ...     ...        ...         ...   \n",
       "585           0        0        1       1       1          1           1   \n",
       "586           0        1        0       0       1          1           1   \n",
       "587           0        1        0       1       1          1           1   \n",
       "588           0        1        1       0       1          1           1   \n",
       "589           0        1        1       1       1          1           1   \n",
       "\n",
       "                               Reaction Format Initial Compounds  \n",
       "0                                   SM -> INT1                SM  \n",
       "1                               SM + R -> INT1              SM,R  \n",
       "2                                   SM -> IMP1           SM,IMP1  \n",
       "3                               SM + R -> IMP1         SM,R,IMP1  \n",
       "4                            SM -> IMP1 + INT1           SM,IMP1  \n",
       "..                                         ...               ...  \n",
       "585      SM + P + C + B + R + INT1 -> IMP2 + B          SM,B,C,R  \n",
       "586          SM + P + C + B + INT1 -> IMP2 + C            SM,B,C  \n",
       "587      SM + P + C + B + R + INT1 -> IMP2 + C          SM,B,C,R  \n",
       "588      SM + P + C + B + INT1 -> IMP2 + C + B            SM,B,C  \n",
       "589  SM + P + C + B + R + INT1 -> IMP2 + C + B          SM,B,C,R  \n",
       "\n",
       "[590 rows x 16 columns]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_combined = df.copy()\n",
    "df_combined"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "37466f85",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "126\n",
      "464\n"
     ]
    }
   ],
   "source": [
    "# Get the indexes of all combinations for the mechanism has two reactions\n",
    "# Filter rows where \"Number of Reactions\" is 2 and \"Step\" is 1\n",
    "condition_1 = (df_combined[\"Number of Reactions\"] == 2) & (df_combined[\"Step\"] == 1)\n",
    "rows_with_2_rxns_1_step = df_combined[condition_1]\n",
    "\n",
    "# Filter rows where \"Number of Reactions\" is 2 and \"Step\" is 2\n",
    "condition_2 = (df_combined[\"Number of Reactions\"] == 2) & (df_combined[\"Step\"] == 2)\n",
    "rows_with_2_rxns_2_step = df_combined[condition_2]\n",
    "\n",
    "# Get the list of indexes for each combination\n",
    "indexes_with_2_rxns_1_step = rows_with_2_rxns_1_step.index.tolist()\n",
    "indexes_with_2_rxns_2_step = rows_with_2_rxns_2_step.index.tolist()\n",
    "\n",
    "print(len(indexes_with_2_rxns_1_step))\n",
    "print(len(indexes_with_2_rxns_2_step))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "3755718c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "First few combinations of indexes:\n",
      "[(0, 126), (0, 127), (0, 128), (0, 129), (0, 130), (0, 131), (0, 132), (0, 133), (0, 134), (0, 135)]\n",
      "Total number of combinations: 58464\n"
     ]
    }
   ],
   "source": [
    "# Create all combinations of the indexes\n",
    "combinations_two_rxns = list(itertools.product(indexes_with_2_rxns_1_step, indexes_with_2_rxns_2_step))\n",
    "\n",
    "# Print the first few combinations to check the result\n",
    "print(\"First few combinations of indexes:\")\n",
    "print(combinations_two_rxns[:10])\n",
    "\n",
    "# Total number of valid combinations\n",
    "print(\"Total number of combinations:\", len(combinations_two_rxns))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f4dffe31",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Valid combinations that satisfy Rule 6:\n",
      "[(0, 130), (0, 131), (0, 132), (0, 133), (0, 134), (0, 135), (0, 136), (0, 137), (0, 146), (0, 147)]\n",
      "Total number of valid combinations: 45504\n"
     ]
    }
   ],
   "source": [
    "# Criteria 6: At least one step of the reactions must involve product formation.\n",
    "\n",
    "valid_combinations_rule6_two_reactions = []\n",
    "\n",
    "# Iterate through each combination\n",
    "for combination in combinations_two_rxns:\n",
    "    idx_row1, idx_row2 = combination\n",
    "\n",
    "    # Check if \"P_right\" is present in either step\n",
    "    if df_combined.loc[idx_row1, \"P_right\"] == 1 or df_combined.loc[idx_row2, \"P_right\"] == 1:\n",
    "        valid_combinations_rule6_two_reactions.append(combination)\n",
    "\n",
    "print(\"Valid combinations that satisfy Rule 6:\")\n",
    "print(valid_combinations_rule6_two_reactions[:10])\n",
    "\n",
    "print(\"Total number of valid combinations:\", len(valid_combinations_rule6_two_reactions))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "0039a0df",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Valid combinations that satisfy Rule 7:\n",
      "[(0, 146), (0, 147), (0, 148), (0, 149), (0, 150), (0, 151), (0, 152), (0, 153), (0, 154), (0, 155)]\n",
      "Total number of valid combinations after applying Rule 7: 23040\n"
     ]
    }
   ],
   "source": [
    "# Criteria 7: If the product or intermediate are reactants in the second step, they must be formed in the first step.\n",
    "\n",
    "valid_combinations_rule7_two_reactions = []\n",
    "\n",
    "for combination in valid_combinations_rule6_two_reactions:\n",
    "    idx_row1, idx_row2 = combination\n",
    "\n",
    "    # Check if P_left, INT_left in the second step correspond to P_right, INT_right in the first step\n",
    "    if (((df_combined.loc[idx_row1, \"P_right\"] == 0 and df_combined.loc[idx_row2, \"P_left\"] == 0 and \\\n",
    "          df_combined.loc[idx_row2, \"P_right\"] == 1) or \\\n",
    "            (df_combined.loc[idx_row1, \"P_right\"] == 1)) and \\\n",
    "                df_combined.loc[idx_row2, \"INT1_left\"] == df_combined.loc[idx_row1, \"INT1_right\"]):\n",
    "\n",
    "        valid_combinations_rule7_two_reactions.append(combination)\n",
    "\n",
    "print(\"Valid combinations that satisfy Rule 7:\")\n",
    "print(valid_combinations_rule7_two_reactions[:10])\n",
    "\n",
    "print(\"Total number of valid combinations after applying Rule 7:\", len(valid_combinations_rule7_two_reactions))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "3b648994",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Valid combinations that satisfy Rule 8:\n",
      "[(0, 146), (0, 147), (0, 154), (0, 155), (0, 200), (0, 201), (0, 208), (0, 209), (0, 254), (0, 255)]\n",
      "Total number of valid combinations after applying Rule 8: 4000\n"
     ]
    }
   ],
   "source": [
    "# Criteria 8: The overall reaction should have the same amount of base, catalyst, and intermediate on both sides of the reaction.\n",
    "\n",
    "def is_valid_overall_reaction(combination):\n",
    "    idx_row1, idx_row2 = combination\n",
    "\n",
    "    # Check if 'C' and 'B' elements are present on the left side of the overall reaction\n",
    "    c_left = df_combined.loc[idx_row1, 'C_left'] + df_combined.loc[idx_row2, 'C_left']\n",
    "    b_left = df_combined.loc[idx_row1, 'B_left'] + df_combined.loc[idx_row2, 'B_left']\n",
    "    c_right = df_combined.loc[idx_row1, 'C_right'] + df_combined.loc[idx_row2, 'C_right']\n",
    "    b_right = df_combined.loc[idx_row1, 'B_right'] + df_combined.loc[idx_row2, 'B_right']\n",
    "\n",
    "    if c_left > 0 or c_right > 0 or b_left > 0 or b_right > 0:\n",
    "        # Ensure that 'C' and 'B' elements have the same amount on the right side\n",
    "        if c_left != c_right or b_left != b_right:\n",
    "            return False\n",
    "\n",
    "    # Check if 'INT1' elements are present on the left side of the overall reaction\n",
    "    int1_left = df_combined.loc[idx_row1, 'INT1_left'] + df_combined.loc[idx_row2, 'INT1_left']\n",
    "    int1_right = df_combined.loc[idx_row1, 'INT1_right'] + df_combined.loc[idx_row2, 'INT1_right']\n",
    "\n",
    "    if int1_left > 0 or int1_right > 0:\n",
    "        # Ensure that 'INT' elements have the same amount on the right side\n",
    "        if int1_left != int1_right:\n",
    "            return False\n",
    "\n",
    "    return True\n",
    "\n",
    "valid_combinations_rule8_two_reactions = [combination for combination in valid_combinations_rule7_two_reactions if is_valid_overall_reaction(combination)]\n",
    "\n",
    "print(\"Valid combinations that satisfy Rule 8:\")\n",
    "print(valid_combinations_rule8_two_reactions[:10])\n",
    "\n",
    "print(\"Total number of valid combinations after applying Rule 8:\", len(valid_combinations_rule8_two_reactions))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "8f95c2e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Valid combinations that satisfy Rule 9-17:\n",
      "[(0, 146), (0, 147), (0, 154), (0, 155), (1, 146), (1, 147), (1, 154), (1, 155), (4, 146), (4, 147)]\n",
      "Total number of valid combinations after applying Rule 9-17: 456\n"
     ]
    }
   ],
   "source": [
    "def is_valid_reaction(combination):\n",
    "    idx_row1, idx_row2 = combination\n",
    "\n",
    "    # Criteria 9. If the starting material and reagent are reactants in the first step, they should not exist in the second step simultaneously.\n",
    "    if df_combined.loc[idx_row1, \"SM_left\"] == 1 and df_combined.loc[idx_row1, \"R_left\"] > 0 and df_combined.loc[idx_row2, \"SM_left\"] == 1 and df_combined.loc[idx_row2, \"R_left\"] > 0:\n",
    "        return False\n",
    "\n",
    "    # Criteria 10: The first step cannot solely form impurity without simultaneously forming product or intermediate.\n",
    "    if df_combined.loc[idx_row1, \"IMP1_right\"] == 1 and df_combined.loc[idx_row1, \"P_right\"] == 0 and df_combined.loc[idx_row1, \"INT1_right\"] == 0:\n",
    "        return False\n",
    "\n",
    "    # Criteria 11: Only one of the two steps can form a product.\n",
    "    if df_combined.loc[idx_row1, \"P_right\"] == 1 and df_combined.loc[idx_row2, \"P_right\"] == 1:\n",
    "        return False \n",
    "    \n",
    "    # Criteria 12: If an intermediate is formed in the first step, the starting material should not be present in the second step. The second step should initiate from the intermediate instead.\n",
    "    if df_combined.loc[idx_row1, \"SM_left\"] == 1 and df_combined.loc[idx_row1, \"INT1_right\"] == 1 and df_combined.loc[idx_row2, \"SM_left\"] == 1:\n",
    "        return False\n",
    "\n",
    "    # Criteria 13: The base has to be present on both sides of reactions if there is any.\n",
    "    if df_combined.loc[idx_row1, \"B_left\"] != df_combined.loc[idx_row1, \"B_right\"] or df_combined.loc[idx_row2, \"B_left\"] != df_combined.loc[idx_row2, \"B_right\"]:\n",
    "        return False\n",
    "\n",
    "    # Criteria 14: If the base is used in the second step, it must also be used in the first step. \n",
    "    # For example, if the first step forms the product and is reacted in the second step in the presence of the base, the base must also be used in the first step for the main reaction.\n",
    "    if df_combined.loc[idx_row2, \"B_left\"] == 1 and df_combined.loc[idx_row1, \"B_right\"] == 0:\n",
    "        return False\n",
    "    \n",
    "    # Criteria 15: If there is no intermediate formed in the first step, \n",
    "    # the reagent and product should be in the same step because the product must be formed from the reagent and starting material.\n",
    "    if df_combined.loc[idx_row1, \"INT1_right\"] != 1 and ((df_combined.loc[idx_row1, \"P_right\"] == 1 and df_combined.loc[idx_row1, \"R_left\"] == 0)\\\n",
    "    or (df_combined.loc[idx_row2, \"P_right\"] == 1 and df_combined.loc[idx_row2, \"R_left\"] == 0)):\n",
    "        return False\n",
    "\n",
    "    # Criteria 16. If the catalyst is not utilized to catalyze the starting material (as not shown on the left side of the first step), it should not be employed in the second step. \n",
    "    # This implies that the catalyst cannot catalyze all other species without catalyzing the starting material and reagent.\n",
    "    if df_combined.loc[idx_row1, \"C_left\"] == 0 and (df_combined.loc[idx_row1, \"C_right\"] == 1 or df_combined.loc[idx_row2, \"C_left\"] == 1):\n",
    "        return False\n",
    "\n",
    "    # Criteria 17. If the product is formed in the first step and the reagent is not a reactant in the first step, the reagent should not be in the second reaction either. \n",
    "    # The reagent must at least react with the starting material.\n",
    "    if df_combined.loc[idx_row1, \"P_right\"] == 1 and (df_combined.loc[idx_row1, \"R_left\"] == 0 and df_combined.loc[idx_row2, \"R_left\"] == 1):\n",
    "        return False   \n",
    "    return True\n",
    "\n",
    "# Filter combinations that satisfy all rules for overall reactions\n",
    "valid_combinations_rule_9_17_two_reactions = [combination for combination in valid_combinations_rule8_two_reactions if is_valid_reaction(combination)]\n",
    "\n",
    "print(\"Valid combinations that satisfy Rule 9-17:\")\n",
    "print(valid_combinations_rule_9_17_two_reactions[:10])\n",
    "\n",
    "print(\"Total number of valid combinations after applying Rule 9-17:\", len(valid_combinations_rule_9_17_two_reactions))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "7f172eb5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Number of Reactions</th>\n",
       "      <th>Step</th>\n",
       "      <th>SM_left</th>\n",
       "      <th>C_left</th>\n",
       "      <th>B_left</th>\n",
       "      <th>P_right</th>\n",
       "      <th>IMP1_right</th>\n",
       "      <th>INT1_right</th>\n",
       "      <th>C_right</th>\n",
       "      <th>B_right</th>\n",
       "      <th>R_left</th>\n",
       "      <th>P_left</th>\n",
       "      <th>INT1_left</th>\n",
       "      <th>IMP2_right</th>\n",
       "      <th>Reaction Format</th>\n",
       "      <th>Initial Compounds</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>SM -&gt; INT1</td>\n",
       "      <td>SM</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>INT1 -&gt; P</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>SM -&gt; INT1</td>\n",
       "      <td>SM</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>R + INT1 -&gt; P</td>\n",
       "      <td>R</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>SM -&gt; INT1</td>\n",
       "      <td>SM</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>301</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>C + B + R + INT1 -&gt; IMP2 + C + B</td>\n",
       "      <td>B,C,R</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>125</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>SM + C + B + R -&gt; P + IMP1 + C + B + INT1</td>\n",
       "      <td>SM,B,C,R,IMP1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>332</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>P + C + B + INT1 -&gt; IMP2 + C + B</td>\n",
       "      <td>B,C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>125</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>SM + C + B + R -&gt; P + IMP1 + C + B + INT1</td>\n",
       "      <td>SM,B,C,R,IMP1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>333</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>P + C + B + R + INT1 -&gt; IMP2 + C + B</td>\n",
       "      <td>B,C,R</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>912 rows × 16 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    Number of Reactions Step SM_left C_left B_left P_right IMP1_right  \\\n",
       "0                     2    1       1      0      0       0          0   \n",
       "146                   2    2       0      0      0       1          0   \n",
       "0                     2    1       1      0      0       0          0   \n",
       "147                   2    2       0      0      0       1          0   \n",
       "0                     2    1       1      0      0       0          0   \n",
       "..                  ...  ...     ...    ...    ...     ...        ...   \n",
       "301                   2    2       0      1      1       0          0   \n",
       "125                   2    1       1      1      1       1          1   \n",
       "332                   2    2       0      1      1       0          0   \n",
       "125                   2    1       1      1      1       1          1   \n",
       "333                   2    2       0      1      1       0          0   \n",
       "\n",
       "    INT1_right C_right B_right R_left P_left INT1_left IMP2_right  \\\n",
       "0            1       0       0      0      0         0          0   \n",
       "146          0       0       0      0      0         1          0   \n",
       "0            1       0       0      0      0         0          0   \n",
       "147          0       0       0      1      0         1          0   \n",
       "0            1       0       0      0      0         0          0   \n",
       "..         ...     ...     ...    ...    ...       ...        ...   \n",
       "301          0       1       1      1      0         1          1   \n",
       "125          1       1       1      1      0         0          0   \n",
       "332          0       1       1      0      1         1          1   \n",
       "125          1       1       1      1      0         0          0   \n",
       "333          0       1       1      1      1         1          1   \n",
       "\n",
       "                               Reaction Format Initial Compounds  \n",
       "0                                   SM -> INT1                SM  \n",
       "146                                  INT1 -> P                    \n",
       "0                                   SM -> INT1                SM  \n",
       "147                              R + INT1 -> P                 R  \n",
       "0                                   SM -> INT1                SM  \n",
       "..                                         ...               ...  \n",
       "301           C + B + R + INT1 -> IMP2 + C + B             B,C,R  \n",
       "125  SM + C + B + R -> P + IMP1 + C + B + INT1     SM,B,C,R,IMP1  \n",
       "332           P + C + B + INT1 -> IMP2 + C + B               B,C  \n",
       "125  SM + C + B + R -> P + IMP1 + C + B + INT1     SM,B,C,R,IMP1  \n",
       "333       P + C + B + R + INT1 -> IMP2 + C + B             B,C,R  \n",
       "\n",
       "[912 rows x 16 columns]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_two_reactions = pd.DataFrame()\n",
    "rows_list = []\n",
    "for idx_row1, idx_row2 in valid_combinations_rule_9_17_two_reactions:\n",
    "    rxn1 = df_combined.iloc[idx_row1, :]\n",
    "    rxn2 = df_combined.iloc[idx_row2, :]\n",
    "    rows_list.append(rxn1)\n",
    "    rows_list.append(rxn2)\n",
    "    df_two_reactions = pd.concat(rows_list, axis=1).T\n",
    "# print(df_two_reactions)\n",
    "df_two_reactions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "aecaad3c",
   "metadata": {},
   "outputs": [],
   "source": [
    "excel_file_path = 'mechanism_two_reactions_paper.xlsx'\n",
    "df_two_reactions.to_excel(excel_file_path, index=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "cf63a903",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['valid_combinations_two_reactions.joblib']"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "joblib.dump(valid_combinations_rule_9_17_two_reactions, \"valid_combinations_two_reactions.joblib\", compress=9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d646bbc9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc51743c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  },
  "vscode": {
   "interpreter": {
    "hash": "8a5edab282632443219e051e4ade2d1d5bbc671c781051bf1437897cbdfea0f1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
